package com.hellacutty.shake;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class CountdownActivity extends Activity{
	private BluetoothChatService _btService;
	private Handler _handler;
	private boolean _otherPlayerReady;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.countdownactivity);
		_otherPlayerReady = false;
		_btService = ((ShakeApplication)getApplication()).getBTService();

		_handler = new Handler(){
			@Override
			public void handleMessage(Message msg){
				switch(msg.what){
				case BluetoothChat.MESSAGE_READ:
					byte[] readBuf = (byte[])msg.obj;
					String readMessage = new String(readBuf, 0, msg.arg1);
					if(readMessage.equals(ProtocolConstants.READY_TO_PLAY)){
						Log.d("JON", "switching otherplayerread");
						_otherPlayerReady = true;
					}else if(readMessage.equals(ProtocolConstants.START_PLAYING)){
//						startActivity(new Intent(getApplicationContext(), CanvasActivity2Player.class));
						startActivity(new Intent(getApplicationContext(), CountdownBeforeStartActivity.class));
					}
					break;
				}
			}
		};
		_btService.setHandler(_handler);

	}

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.ready_to_play:
        	if(_otherPlayerReady){
        		BluetoothHelper.sendMessage(ProtocolConstants.START_PLAYING, _btService);
        		//startActivity(new Intent(getApplicationContext(), CanvasActivity2Player.class));
        		startActivity(new Intent(getApplicationContext(), CountdownBeforeStartActivity.class));
        	}else{
        		BluetoothHelper.sendMessage(ProtocolConstants.READY_TO_PLAY, _btService);
        		Button button = (Button) findViewById(R.id.ready_to_play);
        		button.setEnabled(false);
        		Toast.makeText(this, "Waiting for other player...", Toast.LENGTH_LONG).show();
        	}
        	break;
        }
    }
}
